﻿#include "qlogin.h"
#include "ui_qlogin.h"
#include <QDebug>
#include <QMouseEvent>
#pragma once
#include "mainwindow.h"
#include <QMessageBox>
#include <QFile>
#include "qmpsw.h"
#include "qaccountmanagement.h"

std::map <QString,QString> UserInfo;//账户信息存储
std::map <QString,QString> Usertype;//账户标志
QString defalut_account;
extern MainWindow *g_pmainwindow;
Qlogin::Qlogin(bool management,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Qlogin)
{

    ui->setupUi(this);
    /***********数据初始化**************/
    isadmin = management;
    fun_readUserInfo(); //读取账户信息
    init_dialog(); //对话框初始化
    //Qmpsw::fun_writeUserInfo();
}

Qlogin::~Qlogin()
{
    Qmpsw::fun_writeUserInfo();
    delete ui;
}

void Qlogin::init_dialog(){

    Init_Skin();
    Init_Control();
    Init_SConnect();
}

void Qlogin::Init_Skin(){
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint);
    this->setAttribute (Qt::WA_DeleteOnClose);
    QString str_qss = MyTitleBar::loadStyleSheet(":/qss/black.qss");
    if(str_qss.isEmpty())
    {
        qDebug()<<"qss file false";
    }
    setStyleSheet(str_qss);//加载样式表
    QString qstr_text=QString::fromLocal8Bit("用户登录");
    ui->label->setText(qstr_text);
    ui->label->setAlignment(Qt::AlignCenter);
    move(960-this->width()/2,540-this->height()/2);    //调整窗口出现位置
}

void Qlogin::Init_Control(){
    ui->lineEdit_password->setEchoMode(QLineEdit::Password);
    ui->Button_login->setEnabled(false);
    ui->lineEdit_account->setText(defalut_account);
    if(isadmin == true){
        ui->Buttonchangepassword->setEnabled(true);
        ui->Button_Accountmanagement->setEnabled(true);
    }else{
        ui->Buttonchangepassword->setEnabled(false);
        ui->Button_Accountmanagement->setEnabled(false);
    }
}

void Qlogin::Init_SConnect(){

}

void Qlogin::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
        dragPosition=event->globalPos()-frameGeometry().topLeft();
        event->accept();
    }
    if(event->button()==Qt::RightButton)
    {
        close();
    }
}

void Qlogin::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons()&Qt::LeftButton)
    {
        move(event->globalPos()-dragPosition);
        event->accept();
    }
}


void Qlogin::on_Button_login_clicked()
{
    QString str_account = ui->lineEdit_account->text();
    QString str_password = ui->lineEdit_password->text();
    std::map<QString,QString>::iterator it;
    it  =  UserInfo.find(str_account);
    if (it != UserInfo.end()){
        if(it->second == str_password)
        {
            qDebug()<<"登陆成功";
            defalut_account = str_account;
            int lever = Usertype[str_account].toInt();
            g_pmainwindow->userlever = lever;
            if(lever == 1){
                g_pmainwindow->isadmin = true;
            }else{
                g_pmainwindow->isadmin = false;
            }
            this->accept();
        }else {
            qDebug()<<"登陆失败";
            QMessageBox::information(this,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit("密码错误"));

        }
    }

}

void Qlogin::on_Buttonchangepassword_clicked()
{
    Qmpsw *window_password = new Qmpsw(this);
    window_password->setModal(true);
    window_password->show();
}

void Qlogin::fun_readUserInfo(){
    //思路： 文本第一行储存上次登陆的账户名，第一次登陆时默认为admin
    //      文本第二行开始，每3行为一个账户（三行中第一行为账户名，第二行为密码，第三行为管理员标志，1为管理，0为普通）
    //      无文本时创建文本，包含一个默认管理账户，账户密码都为admin
    UserInfo.clear();
    Usertype.clear();
    QFile file("resource/info.txt");
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    if(file.isOpen())
    {
        //有无密码文本时
        int index =0;
        while (!file.atEnd())
        {
            if(index==0){
                QString str = file.readLine();
                str.remove('\n');
                Qmpsw::Decrypt(str);
                defalut_account = str;
                index++;
            }
            QString  line = file.readLine();
            line.remove('\n');
            Qmpsw::Decrypt(line);
            qDebug()<<"account:"<<line;
            QString  line1 = file.readLine() ;
            line1.remove('\n');
            Qmpsw::Decrypt(line1);
            qDebug()<<"password:"<<line1;
            UserInfo[line]=line1;
            QString  line2 = file.readLine() ;
            line2.remove('\n');
            Qmpsw::Decrypt(line2);
            qDebug()<<"type:"<<line2;
            Usertype[line]=line2;
        }
    }else {
        //无密码文本时
        defalut_account = "admin";
        UserInfo["admin"]="admin";
        Usertype["admin"]="1";
    }
    file.close();
}


void Qlogin::on_Button_cancel_clicked()
{
    this->reject();
}

void Qlogin::on_lineEdit_account_textChanged(const QString &arg1)
{
    if(arg1.size()>1)
    {
        ui->Button_login->setEnabled(true);
    }
    if(arg1.size()<1)
    {
        ui->Button_login->setEnabled(false);
    }
}

void Qlogin::on_Button_Accountmanagement_clicked()
{
    QAccountmanagement *window_Accountmanagement = new QAccountmanagement(this);
    window_Accountmanagement->setModal(true);
    window_Accountmanagement->show();
}


